package jp.co.apl.haven_enquete.android.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import jp.co.apl.haven.generated.HavenProtos.BasicInfo;
import jp.co.apl.haven_enquete.android.dao.BasicInfoDataDao;
import jp.co.apl.haven_enquete.android.db.DatabaseHelper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.google.common.collect.Maps;

public class BasicInfoDataDaoTest extends AndroidTestCase {

    private BasicInfoDataDao target;

    protected void setUp() throws Exception {
        super.setUp();
        target = new BasicInfoDataDao(mContext);
    }

    @SmallTest
    public void testSave() throws Exception {
        DatabaseHelper helper = new DatabaseHelper(mContext);
        {
            SQLiteDatabase db = helper.getWritableDatabase();
            db.execSQL("delete from basic_info");
            db.close();
        }

        Map<Integer, String> map = Maps.newHashMap();
        {
            map.put(1, "A1");
            map.put(2, "A2");
            map.put(3, "A3");
            map.put(4, "A4");
            map.put(5, "A5");
        }
        target.save(map);

        {
            SQLiteDatabase db = helper.getReadableDatabase();
            Cursor cur = db.rawQuery("select * from basic_info", null);
            assertEquals(5, cur.getCount());
            cur.close();
            db.close();
        }

        {
            map.clear();
            map.put(3, "B3");
        }
        target.save(map);

        {
            SQLiteDatabase db = helper.getReadableDatabase();
            Cursor cur = db.rawQuery("select * from basic_info", null);
            assertEquals(5, cur.getCount());
            cur.close();
            db.close();
        }
        {
            SQLiteDatabase db = helper.getReadableDatabase();
            Cursor cur =
                db.rawQuery("select value from basic_info where _id = 3", null);
            cur.moveToFirst();
            assertEquals("B3", cur.getString(0));
            cur.close();
            db.close();
        }
    }

    @SmallTest
    public void testLoad() throws Exception {
        DatabaseHelper helper = new DatabaseHelper(mContext);
        long now = new Date().getTime();
        {
            SQLiteDatabase db = helper.getWritableDatabase();
            db.execSQL("delete from basic_info");
            db.execSQL("insert into basic_info values (10, 'A10'," + now + ")");
            db.execSQL("insert into basic_info values (11, 'A11'," + now + ")");
            db.execSQL("insert into basic_info values (12, 'A12'," + now + ")");
            db.execSQL("insert into basic_info values (13, 'A13'," + now + ")");
            db.execSQL("insert into basic_info values (14, 'A14'," + now + ")");
            db.close();
        }

        List<BasicInfo> list = target.load();

        assertNotNull(list);
        assertEquals(5, list.size());
        assertEquals(10, list.get(0).getBasicInfoItemId());
        assertEquals(11, list.get(1).getBasicInfoItemId());
        assertEquals(12, list.get(2).getBasicInfoItemId());
        assertEquals(13, list.get(3).getBasicInfoItemId());
        assertEquals(14, list.get(4).getBasicInfoItemId());
        assertEquals("A10", list.get(0).getAnswer());
        assertEquals("A11", list.get(1).getAnswer());
        assertEquals("A12", list.get(2).getAnswer());
        assertEquals("A13", list.get(3).getAnswer());
        assertEquals("A14", list.get(4).getAnswer());
        assertEquals(now, list.get(0).getAnsweredDate());
        assertEquals(now, list.get(1).getAnsweredDate());
        assertEquals(now, list.get(2).getAnsweredDate());
        assertEquals(now, list.get(3).getAnsweredDate());
        assertEquals(now, list.get(4).getAnsweredDate());
    }
}
